// index.js
const util = require('../../../util/util.js')
// const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({
  data: {
    dataTable: [], // 原始车辆数据
    carTypeCounts: [], // 分类统计结果
    timeStart: null, //时分秒，根据需要选择
    CLIENT_NO: '',
    clientNoIndex: 0,
    clientNoList: [],
  },
  onLoad: function () {
    var that = this;
    const currentDate = new Date();
    currentDate.setDate(currentDate.getDate() - 1);
    var CLIENT_NO = wx.getStorageSync("CLIENT_NO");
    that.setData({
      timeStart: util.formatDate(currentDate),
      CLIENT_NO: CLIENT_NO
    })

    this.getClientNoListInfo()
  },

  // 获取组织机构信息
  getClientNoListInfo() {
    var that = this;
    wx.request({
      url: 'https://www.ejingyou.cn/ServerHuanwei/CompanyapiBDEapi/Vehicle/GetClientInfo',
      data: {
        client_no: this.data.CLIENT_NO,
      },
      header: {
        'content-type': 'application/json' // 默认值     
      },
      success: (res) => {
        // 转换原始数据并初始化
        const originalData = res.data;
        const pickerData = originalData.map(item => ({
          value: item.CLIENT_GROUP_NO,
          label: item.CLIENT_GROUP_NAME
        }));
        if (this.data.CLIENT_NO === "00000") {
          const newItem = {
            value: "00000",
            label: "全市"
          };
          pickerData.unshift(newItem);
        }
        this.setData({
          clientNoList: pickerData,
          CLIENT_NO: pickerData[0].value // 默认选中第一个
        });
        this.getCarRealtimeInfo()
      }
    })
  },
  // 获取信息
  getCarRealtimeInfo() {
    wx.request({
      url: 'https://www.ejingyou.cn/ServerHuanwei/CompanyapiBDEapi/Vehicle/GetExcelCar1',
      data: {
        clientNo: this.data.CLIENT_NO,
        date: this.data.timeStart,
        carType: ''
      },
      header: {
        'content-type': 'application/json' // 默认值     
      },
      success: (res) => {
        // console.log(res.data)
        const originalData = res.data.slice(1);


        // 分类统计（增加去空格处理）
        const counts = originalData.reduce((acc, cur) => {
          const type = cur.CAR_TYPE.trim(); // 去除前后空格防止统计错误
          acc[type] = (acc[type] || 0) + 1;
          return acc;
        }, {});

        // 转换为数组并按数量排序
        this.setData({
          carTypeCounts: Object.keys(counts)
            .map(type => ({
              type,
              count: counts[type]
            }))
            .sort((a, b) => b.count - a.count) // 按数量降序
        });

        if (this.data.typeF) {
          this.setData({
            typeF: false
          })
        }

        if (this.data.typeC) {
          this.setData({
            typeC: false
          })
        }
      }
    })
  },
  onTypeTap(e) {
    const type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: '../carDeploymentNo/carDeploymentNo?type=' + type
    });
  },


  openTypeF() {
    this.setData({
      typeF: true
    })
  },

  openTypeC() {
    console.log(this.data.clientNoList)
    this.setData({
      typeC: true
    })
  },

  selectDateStartChange(e) {
    this.setData({
      timeStart: e.detail.value
    })
  },

  selectClientNoChange(e) {
    const index = e.detail.value;
    const CLIENT_NO = this.data.clientNoList[index].value;
    this.setData({
      clientNoIndex: index,
      CLIENT_NO
    });
  },

  getDateTimeChange() {
    this.getCarRealtimeInfo(this.data.activeTab)
  },
})